package com.cw.java8.optional.demo;

import java.util.Optional;

/**
 * 用户Service
 */
public class UserService {

    private UserDao userDao = new UserDao();

    public String findUserName(Integer id) {
        User user = userDao.findUserById(id);
        //大量的防御式编程
        if (user!=null) {
            String userName = user.getUserName();
            if(userName!=null && !"".equals(userName)){
                return userName;
            }
        }
        return "Unkown";
    }

    public String findUserNameByOptional(Integer id) {
        User user = userDao.findUserById(id);
        Optional<User> optionalUser = Optional.ofNullable(user);
        return optionalUser.map(u -> {
            return u.getUserName();
        }).orElse("Unkown");
    }
}
